package com.muyu.danmo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCommunity;
import com.muyu.danmo.domain.DanmoTrainingFavorite;
import com.muyu.danmo.domain.DanmoTrainingInformation;
import com.muyu.danmo.service.IDanmoTrainingFavoriteService;
import com.muyu.danmo.service.IDanmoTrainingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 培训班收藏Controller
 * 
 * @author kunzong
 * @date 2024-11-27
 */
@RestController
@RequestMapping("/DanmoTrainingFavorite")
public class DanmoTrainingFavoriteController extends BaseController
{
    @Autowired
    private IDanmoTrainingFavoriteService danmoTrainingFavoriteService;

    @Autowired
    private IDanmoTrainingInformationService danmoTrainingInformationService;

    /**
     * 查询培训班收藏列表
     */
    @RequiresPermissions("system:DanmoTrainingFavorite:list")
    @GetMapping("/list")
    public Result list(DanmoTrainingFavorite danmoTrainingFavorite)
    {
        startPage();
        List<DanmoTrainingFavorite> list = danmoTrainingFavoriteService.selectDanmoTrainingFavoriteList(danmoTrainingFavorite);
        return getDataTable(list);
    }

    /**
     * 导出培训班收藏列表
     */
    @RequiresPermissions("system:DanmoTrainingFavorite:export")
    @Log(title = "培训班收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoTrainingFavorite danmoTrainingFavorite)
    {
        List<DanmoTrainingFavorite> list = danmoTrainingFavoriteService.selectDanmoTrainingFavoriteList(danmoTrainingFavorite);
        ExcelUtil<DanmoTrainingFavorite> util = new ExcelUtil<DanmoTrainingFavorite>(DanmoTrainingFavorite.class);
        util.exportExcel(response, list, "培训班收藏数据");
    }

    /**
     * 获取培训班收藏详细信息
     */
    @RequiresPermissions("system:DanmoTrainingFavorite:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return success(danmoTrainingFavoriteService.selectDanmoTrainingFavoriteById(id));
    }

    /**
     * 新增培训班收藏
     */
    @RequiresPermissions("system:DanmoTrainingFavorite:add")
    @Log(title = "培训班收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoTrainingFavorite danmoTrainingFavorite)
    {
        return toAjax(danmoTrainingFavoriteService.insertDanmoTrainingFavorite(danmoTrainingFavorite));
    }

    /**
     * 修改培训班收藏
     */
    @RequiresPermissions("system:DanmoTrainingFavorite:edit")
    @Log(title = "培训班收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoTrainingFavorite danmoTrainingFavorite)
    {
        return toAjax(danmoTrainingFavoriteService.updateDanmoTrainingFavorite(danmoTrainingFavorite));
    }

    /**
     * 删除培训班收藏
     */
    @RequiresPermissions("system:DanmoTrainingFavorite:remove")
    @Log(title = "培训班收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return Result.success(danmoTrainingFavoriteService.deleteDanmoTrainingFavoriteByIds(ids));
    }


    /**
     * 培训班收藏
     */
    @Log(title = "培训班收藏", businessType = BusinessType.UPDATE)
    @GetMapping("/isCollect/{id}")
    public Result edit(@PathVariable Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoTrainingFavoriteService.isCollect(id);
    }


    /**
     * 查询自己收藏的培训班
     * @param danmoTrainingInformation
     * @return
     */

    @PostMapping("/myCollectionTrainingCourses")
    public Result myCollectionCommunity(@RequestBody DanmoTrainingInformation danmoTrainingInformation) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoTrainingInformation.setUserId(loginUser.getUserid());
        PageHelper.startPage(danmoTrainingInformation.getPageNum(), danmoTrainingInformation.getPageSize());
        List<DanmoTrainingInformation> list = danmoTrainingFavoriteService.selectOtherCollectionDanmoCommunityList(danmoTrainingInformation);
        if (list.size() == 0) {
            return Result.error("暂无收藏");
        }
        PageInfo<DanmoTrainingInformation> danmoCommunityPageInfo = new PageInfo<>(list);
        return Result.success(danmoCommunityPageInfo);
    }
}
